<?php
namespace app\api\controller;

use app\api\model\Banner;
use app\api\model\News as NewsModel;

class News extends Auth
{
    /**
     * 首页帖子列表
     * @author dong
    */
    public function index()
    {
        $map = [];
        if (!empty($this->request->param('modular'))) {
            $map['f.modular'] = $this->request->param('modular');
        }
        if (!empty($this->request->param('second_id'))) {
            $map['n.second_id'] = $this->request->param('second_id');
        }
        if (!empty($this->request->param('father_id'))) {
            $map['n.father_id'] = $this->request->param('father_id');
        }
        $list = NewsModel::alias('n')
            ->where('n.status', 1)
            ->where($map)
            ->join('jk_user_informations m', 'm.user_id = n.user_id', 'LEFT')
            ->join('jk_news_types f', 'f.id = n.father_id', 'LEFT') //一级分类
            ->join('jk_news_types s', 's.id = n.second_id', 'LEFT') //二级分类
            ->join('jk_news_types t', 't.id = n.type_id', 'LEFT') //三级分类
            ->field('n.id, n.title, n.create_at, m.nickname, n.user_id as user_icon_url, f.modular, f.name as father_name, s.name as son_name, t.name as sun_name, n.father_id, n.second_id, n.type_id')
            ->paginate($this->number);
        // dump(json_decode( json_encode( $list),true));exit;
        $list = formatPage('newsItem', $list);

        return success($list, '数据获取成功');
    }

    /**
     * 帖子详情
     * @author dong
    */
    public function detail($id='')
    {

        if(empty($id) || $id==''){

            return error('帖子ID不能为空');
        }

        $detail = NewsModel::alias('n')
            ->where('n.id', $id)
            ->join('jk_user_informations m', 'm.user_id = n.user_id', 'LEFT')
            ->field('n.id, n.title, n.create_at, n.text, n.comment_number, m.nickname, n.user_id as user_icon_url, n.zan')
            ->find();
        NewsModel::where('id', $id)->setInc('browse_number');   //浏览量+1
        dump(json_decode( json_encode( $detail),true));exit;

        return success($detail, '数据获取成功');
    }
}
